package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//系统公告相关,游戏活动公告,子游戏大厅里游戏活动公告,子游戏大厅里游戏招募代理公告

func NewNotice(data map[string]interface{}, tableName string) (int64, error) {
	o := NewDBOrm(DBChessSysData)
	values, sql := InsertSql(tableName, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

func UpdateNotice(data map[string]interface{}, id int64, tableName string) (int64, error) {
	o := NewDBOrm(DBChessSysData)
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(tableName, data, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

func DeleteNotice(id int64, tableName string) (int64, error) {
	o := NewDBOrm(DBChessSysData)
	condition := fmt.Sprintf("id=%v", id)
	sql := DeleteSql(tableName, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

func AgentNoticeInfoById(gameId int64) (*AgentNotice, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			id = ?
		LIMIT 1
		`, TableAgentNotice)
	o := NewDBOrm(DBChessSysData)
	var notice AgentNotice
	err := o.Raw(sql, gameId).QueryRow(&notice)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &notice, err
}

func GameNoticeInfoById(id int64) (*GameNotice, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			id = ?
		LIMIT 1
		`, TableGameNotice)
	o := NewDBOrm(DBChessSysData)
	var notice GameNotice
	err := o.Raw(sql, id).QueryRow(&notice)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &notice, err
}

func H5NoticeInfoById(id int64) (*H5Notice, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			id = ?
		LIMIT 1
		`, TableH5Notice)
	o := NewDBOrm(DBChessSysData)
	var notice H5Notice
	err := o.Raw(sql, id).QueryRow(&notice)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &notice, err
}

func ActivityNoticeInfoById(id int64) (*ActivityNotice, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			id = ?
		LIMIT 1
		`, TableActivityNotice)
	o := NewDBOrm(DBChessSysData)
	var notice ActivityNotice
	err := o.Raw(sql, id).QueryRow(&notice)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &notice, err
}

func IsAgentNoticeExistByGameId(gameId int64) bool {
	sql := fmt.Sprintf(`
		SELECT
			game_id
		FROM
			%v
		WHERE
			game_id = ?
		LIMIT 1
		`, TableAgentNotice)
	o := NewDBOrm(DBChessSysData)
	var id int64
	err := o.Raw(sql, gameId).QueryRow(&id)
	if CheckNoExist(err) {
		return false
	}
	if id == gameId {
		return true
	}
	return false
}

func ListActivityNotice(p ListParams) ([]*ActivityNotice, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		ORDER BY
			id DESC
		LIMIT ?,?
		`, TableActivityNotice)
	o := NewDBOrm(DBChessSysData)
	var gl []*ActivityNotice
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&gl)
	if CheckError(err) {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TableActivityNotice)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return gl, count, err
}

func ListAgentNotice(p ListParams) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			n.*,g.name AS game_name
		FROM
			%v AS n
		LEFT JOIN
			%v AS g
		ON n.game_id = g.game_id
		LIMIT ?,?
		`, TableAgentNotice, TableGameList)
	o := NewDBOrm(DBChessSysData)
	var gl []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&gl)
	if CheckNoExist(err) {
		return nil, 0, nil
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TableAgentNotice)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return gl, count, err
}

func ListGameNotice(p ListParams) ([]*GameNotice, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			n.*,g.name AS game_name
		FROM
			%v AS n
		LEFT JOIN
			%v AS g
		ON n.game_id = g.game_id
		LIMIT ?,?
		`, TableGameNotice, TableGameList)
	o := NewDBOrm(DBChessSysData)
	var gl []*GameNotice
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&gl)
	if CheckNoExist(err) {
		return nil, 0, nil
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TableGameNotice)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return gl, count, err
}

func ListH5Notice(p ListParams) ([]*H5Notice, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		LIMIT ?,?
		`, TableH5Notice)
	o := NewDBOrm(DBChessSysData)
	var gl []*H5Notice
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&gl)
	if CheckNoExist(err) {
		return nil, 0, nil
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TableH5Notice)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return gl, count, err
}
